/**
 * Created by zpx on 2017/4/26.
 */
var Draw = {
    set:function (beginPoint,stopPoint){
        init(this,beginPoint,stopPoint);
    },
    paint:function (ctx){
        paint(this,ctx);
    }
};
var init  = function init(obj,beginPoint,stopPoint){
    obj.sp = beginPoint;
    obj.ep = stopPoint;
};
var paint = function drawAl(obj,ctx){
    ctx.beginPath();
    ctx.moveTo(obj.sp.x,obj.sp.y);
    ctx.lineTo(obj.ep.x,obj.ep.y);
    var h = _calcH(obj,obj.sp,obj.ep);
    ctx.moveTo(obj.ep.x,obj.ep.y);
    ctx.lineTo(h.h1.x,h.h1.y);
    ctx.moveTo(obj.ep.x,obj.ep.y);
    ctx.lineTo(h.h2.x,h.h2.y);
    ctx.stroke();
};
var _calcH=function(obj){
    var theta=Math.atan((obj.ep.x-obj.sp.x)/(obj.ep.y-obj.sp.y));
    var cep=_scrollXOY(obj.ep,-theta);
    var csp=_scrollXOY(obj.sp,-theta);
    var ch1={x:0,y:0};
    var ch2={x:0,y:0};
    var l=cep.y-csp.y;
    ch1.x=cep.x+l*(obj.sharp||0.025);
    ch1.y=cep.y-l*(obj.size||0.05);
    ch2.x=cep.x-l*(obj.sharp||0.025);
    ch2.y=cep.y-l*(obj.size||0.05);
    var h1=_scrollXOY(ch1,theta);
    var h2=_scrollXOY(ch2,theta);
    return {h1:h1,h2:h2};
};

var _scrollXOY=function(p,theta){
    return {
        x:p.x*Math.cos(theta)+p.y*Math.sin(theta),
        y:p.y*Math.cos(theta)-p.x*Math.sin(theta)
    };
};


function drawLine(event){
    var oc = document.getElementById("selectedCanvas");
    var ctx  = oc.getContext("2d");
    ctx.fillStyle = "#000000";
    ctx.beginPath();
    //确定位置
    var mouseXY = getMouseXY(event);
    if(stopPoint==null){
        stopPoint = {};
    }
    stopPoint.x = mouseXY.mouseX;
    stopPoint.y = mouseXY.mouseY;
    var CONST = {edgeLen: 50,angle: 25}
    var angle;
    var polygonVertex = [];
    polygonVertex[0] = beginPoint.x+beginPoint.width/2;
    polygonVertex[1] = beginPoint.y+beginPoint.height/2;

    polygonVertex[6] = stopPoint.x;
    polygonVertex[7] = stopPoint.y;

    angle = Math.atan2(stopPoint.y - beginPoint.y, stopPoint.x - beginPoint.x) / Math.PI * 180;
    var x = stopPoint.x - beginPoint.x;
    var y = stopPoint.y - beginPoint.y;
    var length = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
    if (length < 250) {
        CONST.edgeLen = CONST.edgeLen/2;
        CONST.angle = CONST.angle/2;
    }else if(length<500){
        CONST.edgeLen=CONST.edgeLen*length/500;
        CONST.angle=CONST.angle*length/500;
    }
    polygonVertex[8] = stopPoint.x - CONST.edgeLen * Math.cos(Math.PI / 180 * (angle + CONST.angle));
    polygonVertex[9] = stopPoint.y - CONST.edgeLen * Math.sin(Math.PI / 180 * (angle + CONST.angle));
    polygonVertex[4] = stopPoint.x - CONST.edgeLen * Math.cos(Math.PI / 180 * (angle - CONST.angle));
    polygonVertex[5] = stopPoint.y - CONST.edgeLen * Math.sin(Math.PI / 180 * (angle - CONST.angle));

    var midpoint = {};
    midpoint.x=(polygonVertex[4]+polygonVertex[8])/2;
    midpoint.y=(polygonVertex[5]+polygonVertex[9])/2;
    polygonVertex[2] = (polygonVertex[4] + midpoint.x) / 2;
    polygonVertex[3] = (polygonVertex[5] + midpoint.y) / 2;
    polygonVertex[10] = (polygonVertex[8] + midpoint.x) / 2;
    polygonVertex[11] = (polygonVertex[9] + midpoint.y) / 2;

    ctx.moveTo(polygonVertex[0], polygonVertex[1]);
    ctx.lineTo(polygonVertex[2], polygonVertex[3]);
    ctx.lineTo(polygonVertex[0], polygonVertex[1]);
    console.log(polygonVertex);

    ctx.closePath();
    ctx.fill();
}

